home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ZED3DSRC.ZIP / 3DS.C next >
C/C++ Source or Header  |  1995-06-19  |  3KB  |  186 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <3ds.h>
  5. #include <ctype.h>
  6.  
  7.  
  8. int read_line(char *s, FILE *f);
  9.  
  10. int read_line(char *s, FILE *f)
  11.     {
  12.     if(fgets(s,250,f)==0)
  13.         return -1;
  14.  
  15.     s[strlen(s)-1]=0;
  16.     return 0;
  17.     }
  18.  
  19.  
  20. object *read_3ds_file(char *filename, int print_progress)
  21.     {
  22.     FILE *f;
  23.     float A,B,C;
  24.     long a,b,c,x,y,z;
  25.     char s[256];
  26.     object *o;
  27.     face *fa;
  28.     long *index;
  29.  
  30.  
  31.     f=fopen(filename,"r");
  32.     if(!f)
  33.         {
  34.         return 0; /* error */
  35.         }
  36.  
  37.     do
  38.         {
  39.         if(read_line(s,f))
  40.             return 0;
  41.         }
  42.     while(memcmp(s,"Tri-mesh",8));
  43.  
  44.     for(a=0;s[a]!=':';a++) {}
  45.     a+=2;
  46.     x=atol(s+a);
  47.  
  48.     for(;s[a]!=':';a++) {}
  49.     a+=2;
  50.     y=atol(s+a);
  51.  
  52.     index=malloc(sizeof(long)*y*3);
  53.     if(!index)
  54.         return 0;
  55.  
  56.     o=alloc_object(x,y);
  57.     if(!o)
  58.         {
  59.         free(index);
  60.         return 0;
  61.         }
  62.  
  63.     /* ok now read verticies */
  64.  
  65.     if(read_line(s,f))
  66.         {
  67.         free_object(o);
  68.         return 0;
  69.         }
  70.  
  71.     if(print_progress)
  72.         {
  73.         printf("Now reading file %s, %li vertices and %li faces.\n",
  74.             filename,x,y);
  75.         }
  76.  
  77.     for(a=0;a<x;a++)
  78.         {
  79.         if(read_line(s,f))
  80.             {
  81.             free_object(o);
  82.             return 0;
  83.             }
  84.         if(memcmp(s,"Vertex",6))
  85.             {
  86.             a--;
  87.             continue;
  88.             }
  89.         b=atol(s+7);
  90.         if(print_progress)
  91.             {
  92.             if(!(a&127))
  93.                 {
  94.                 printf("Vertex #%li\n",b);
  95.                 }
  96.             }
  97.         if(a!=b)
  98.             {
  99.             if(print_progress)
  100.                 puts("Vertex discrepancy");
  101.             free_object(o);
  102.             return 0;
  103.             }
  104.         for(c=0;s[c]!='X';c++) {}
  105.         A=strtod(s+c+2,0);
  106.         for(;s[c]!='Y';c++) {}
  107.         B=strtod(s+c+2,0);
  108.         for(;s[c]!='Z';c++) {}
  109.         C=strtod(s+c+2,0);
  110.  
  111.         o->pts_data->vertex[b].location[0]=floattoreal(A);
  112.         o->pts_data->vertex[b].location[1]=floattoreal(B);
  113.         o->pts_data->vertex[b].location[2]=floattoreal(C);
  114.         }
  115.  
  116.     if(read_line(s,f))
  117.         {
  118.         free_object(o);
  119.         return 0;
  120.         }
  121.  
  122.     if(print_progress)
  123.         puts("Reading faces");
  124.     fa=o->face_data->face;
  125.     for(a=0;a<y;a++)
  126.         {
  127.         if(read_line(s,f))
  128.             {
  129.             free_object(o);
  130.             return 0;
  131.             }
  132.         if(memcmp(s,"Face",4))
  133.             {
  134.             a--;
  135.             continue;
  136.             }
  137.  
  138.         b=atol(s+5);
  139.         if(b!=a)
  140.             {
  141.             if(print_progress)
  142.                 puts("Face discrepancy");
  143.             free_object(o);
  144.             }
  145.         fa->index=index;
  146.         if(print_progress)
  147.             {
  148.             if(!(a&127))
  149.                 {
  150.                 printf("Face #%li\n",b);
  151.                 }
  152.             }
  153.  
  154.         for(c=5;s[c]!='A';c++) {}
  155.         c+=2;
  156.         fa->index[2]=atol(s+c);
  157.         for(;s[c]!='B';c++) {}
  158.         c+=2;
  159.         fa->index[1]=atol(s+c);
  160.         for(;s[c]!='C';c++) {}
  161.         c+=2;
  162.         fa->index[0]=atol(s+c);
  163.  
  164.         fa->numpoints=3;
  165.  
  166.         index+=3;
  167.         fa++;
  168.         }
  169.  
  170.     fclose(f);
  171.  
  172.     if(print_progress)
  173.         puts("Done reading. Initializing normals");
  174.     init_normals(o);
  175.     normalize_object(o);
  176.  
  177.     if(print_progress)
  178.         puts("Done.");
  179.  
  180.     return o;
  181.     }
  182.  
  183.  
  184.  
  185.  
  186.